<?php
header("Content-Type: text/html; charset=iso-8859-1");
ini_set("memory_limit","32M");
session_start();
require('include.php');
if(check_admin()){
	$id = (!empty($_REQUEST['id'])) ? $_REQUEST['id'] : "";
	$id_equipe = (!empty($_REQUEST['equipe'])) ? $_REQUEST['equipe'] : "";
	$team = new equipe($id_equipe);
	$equipe = $team->id_vbcll;
	if($id){
		//définition des variables
		// error_log("$equipe");
		if(file_exists("photos/equipes/".$equipe."-tmp.jpg")) unlink("photos/equipes/".$equipe."-tmp.jpg");
		$base = "photos/equipes/";
		$dest = "photos/";
		$imgfile = $base.$equipe."-grand.jpg";
		$dest_small = $dest."$id.jpeg";
		$cropStartX = $_POST['sx']*2;
		$cropStartY = $_POST['sy']*2;
		$cropW = $_POST['ex']*2;
		$cropH = $_POST['ey']*2;

		// création des deux images temporaires
		$origimg = imagecreatefromjpeg($imgfile);
		$cropimg = imagecreatetruecolor($cropW,$cropH);

		// résolution de l'image
		list($width, $height) = getimagesize($imgfile);

		// Crop
		imagecopyresized($cropimg, $origimg, 0, 0, $cropStartX, $cropStartY, $width, $height, $width, $height);

		// copie vers répertoire
		imagejpeg($cropimg, $dest_small, 100);

		// Début du redimensionnement
		$thumb = imagecreatetruecolor(100, 100);
		$source = imagecreatefromjpeg($dest_small);
		imagecopyresized($thumb, $source, 0, 0, 0, 0, 100, 100, $cropW, $cropH);

		// copie vers répertoire
		imagejpeg($thumb, $dest_small, 100);
		// Fin du redimmensionnement
		$message = "ok";
	}
	$onload = 'onLoad="fnOnLoad();" onMouseDown="fnOnMouseDown();" onMouseUp="fnOnMouseUp();" ';
	echo "<html><head>\n";
	#header("Content-Type: text/html; charset=utf-8");
	echo '<script src="javascripts/recadrer-image-javascript.js" type="text/javascript" ></script>'."\n";
	echo "<script type = 'text/javascript'>\n";
	echo "function writediv(mytext,mydiv){\n";
	echo "	if(el = opener.window.getElementById(mydiv)) el.innerHTML = mytext;\n";
	echo "}\n";
	echo "</script>\n";
	echo "<link rel='stylesheet' href='styles/vbcll.css' type = 'text/css' />\n";
	echo "</head><body $onload>\n";
	// Le fichier
	$filename = "photos/equipes/".$equipe."-grand.jpg";
	$percent = 0.5;
    
	// Calcul des nouvelles dimensions
	list($width, $height) = getimagesize($filename);
	$new_width = $width * $percent;
	$new_height = $height * $percent;
	// Redimensionnement
	$image_p = imagecreatetruecolor($new_width, $new_height);
	$image = imagecreatefromjpeg($filename);
	imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
	// Affichage
	imagejpeg($image_p,"photos/equipes/".$equipe."-tmp.jpg" , 100);
	$img = getimagesize("photos/equipes/".$equipe."-tmp.jpg");
	// echo "<h1 class = 'center'>$team->id_vbcll : $team->nom_complet</h1>\n";
	echo "<div id='conteneur' style='background: url(\"photos/equipes/".$equipe."-tmp.jpg\") no-repeat; position: relative; width: $img[0]px; height: $img[1]px;'>\n";
    
	echo "<div id='bloc_recadre' name='bloc_recadre' onMouseOver='fnOnMouseOver(\"bloc_recadre\", \"conteneur\");' onMouseOut='fnOnMouseOver();'></div> \n";
	echo "</div>\n";
	echo "<div style = 'position: relative'>\n";
	echo "<form action='crop_photo_equipe.php' method='post'>\n";
	echo "<p class = 'center'><input type='hidden' id='sx' name='sx' value='' />\n";
	echo "<input type='hidden' id='sy' name='sy' value='' />\n";
	echo "<input type='hidden' id='ex' name='ex' value='' />\n";
	echo "<input type='hidden' id='ey' name='ey' value='' />\n";
	echo "<input type = 'hidden' name = 'equipe' value = '$team->id' /></div>\n";		
	$membres = $team->list_membres();
	echo "<div style = 'position: absolute; left: ".($img[0] + 20)."px; top: 20px;'><p class = 'center'><ul class = 'nav'>";
	foreach($membres as $membre_id => $membre){
		if($membre->nom){
			$print = ($membre_id == $id) ? "<span class = 'non'>&nbsp;$message</span>" : "";
			echo "<li><button type = 'submit' name = 'id' value = '$membre_id'>".(htmlentities($membre->prenom))." ".(substr($membre->nom,0,1)).".</button>$print</li>\n";			
		}
	}
	echo "</ul></p></div>\n";
	echo "</form>\n";
	echo "<p class = 'center'><button type = 'button' onclick = 'self.close()'>fermer</button></p>\n";
	echo "</body></html>	\n";
	
}
?>	
